package controller;

import implementor.CampaignImp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.swing.JOptionPane;

import daoInterfaces.CampaignDao;

import model.CampaigMgmtModel;
import utility.ViewUtilites;
import view.campaign.CampaignManagment;

public class CampaignMgmtController implements ActionListener{
	private CampaignManagment view;
	private CampaigMgmtModel model;
	
	
	public CampaignMgmtController() {
	
	}

	public CampaignMgmtController(CampaignManagment view, CampaigMgmtModel model) {
		super();
		this.view = view;
		this.model = model;
		view.actionListener(this);
		
		
	}

	public CampaignManagment getView() {
		return view;
	}

	public void setView(CampaignManagment view) {
		this.view = view;
	}

	public CampaigMgmtModel getModel() {
		return model;
	}

	public void setModel(CampaigMgmtModel model) {
		this.model = model;
	}

	public void reactOnSave()
	{
		String campName=view.gettCampName().getText();
		String createdBy=view.gettCreatedBy().getText();
		Date startDate=ViewUtilites.convert(view.gettStartDate().getText());
		Date endDate=ViewUtilites.convert(view.gettEndDate().getText());
		String descrip=view.getTextAreaDescription().getText();
		
		model=new CampaigMgmtModel(0, campName, startDate, endDate, descrip, createdBy, 1);
		CampaignDao cd=new CampaignImp();
		try
		{
			cd.add(model);
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
		
		
	}
	
	public void reactOnMarkInactive()
	{
		String campname=view.gettCampName().getText();
		CampaignDao campdao=new CampaignImp();
		CampaigMgmtModel c=campdao.retrieve("campaignName", campname);
		c.setStatus(0);
	}
	
	public void reactOnUpdate()
	{
		String campname=view.gettCampName().getText();
		this.reactOnSearch();
		CampaignDao cd=new CampaignImp();
		CampaigMgmtModel c=cd.retrieve("campaignName", campname);
		Date endDate=ViewUtilites.convert(view.gettEndDate().getText());
		c.setEndDate(endDate);
		c.setDescription(view.getTextAreaDescription().getText());
		try
		{
			cd.update(c);
			JOptionPane.showMessageDialog(null, "Data successfully updated");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}
		
	}
	public void reactOnSearch()
	{
		String campname=view.gettCampName().getText();
		CampaignDao cd=new CampaignImp();
		CampaigMgmtModel c=cd.retrieve("campaignName", campname);
		view.gettCreatedBy().setText(c.getCreatedBy());
		view.gettStartDate().setText(c.getStartDate()+"");
		view.gettEndDate().setText(c.getEndDate()+"");
		view.getTextAreaDescription().setText(c.getDescription());
		
		
	}
	
	public void reactOnBack()
	{
		CampaignDao cd=new CampaignImp();
		CampaigMgmtModel[] c=cd.retrieveAll();
		String campname=view.gettCampName().getText();
		int i=c.length-1;
		while(i>0)
		{
			i--;
			if(c[i].getCampaignName().equals(campname))
			{
				if(i==0)
				{
					break;
				}
				else
				{
					i--;
					CampaigMgmtModel cmodel=cd.retrieve("campaignName", c[i].getCampaignName());
					view.gettCampName().setText(cmodel.getCampaignName());
					view.gettCreatedBy().setText(cmodel.getCreatedBy());
					view.gettStartDate().setText(cmodel.getStartDate()+"");
					view.gettEndDate().setText(cmodel.getEndDate()+"");
					view.getTextAreaDescription().setText(cmodel.getDescription());
				}
			}
			
			
		}
		
		
	}
	
	
	
	public void reactOnForward()
	{
		CampaignDao cd=new CampaignImp();
		CampaigMgmtModel[] c=cd.retrieveAll();
		String campname=view.gettCampName().getText();
		int i=0;
		while(i<c.length)
		{
			i++;
			if(c[i].getCampaignName().equals(campname))
			{
				if(i==c.length-1)
				{
					break;
				}
				else
				{
					i++;
					CampaigMgmtModel cmodel=cd.retrieve("campaignName", c[i].getCampaignName());
					view.gettCampName().setText(cmodel.getCampaignName());
					view.gettCreatedBy().setText(cmodel.getCreatedBy());
					view.gettStartDate().setText(cmodel.getStartDate()+"");
					view.gettEndDate().setText(cmodel.getEndDate()+"");
					view.getTextAreaDescription().setText(cmodel.getDescription());
				}
			}	
		}
	}
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String k=arg0.getActionCommand();
		if(k.contains("SAVE"))
		{
			reactOnSave();
		}
		if(k.contains("MARK INACTIVE"))
		{
			reactOnMarkInactive();
		}
		if(k.contains("UPDATE"))
		{
			reactOnUpdate();
		}
		if(k.contains("<<"))
		{
			reactOnBack();
		}
		if(k.contains("SEARCH"))
		{
			reactOnSearch();
		}
		if(k.contains(">>"))
		{
			reactOnForward();
		}
		if(k.contains("CLEAR"))
		{
			
		}
		if(k.contains("EXIT "))
		{
			
		}
		
	}
}
